/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_ENTITY_HPP
#define XERN_ENTITY_HPP

#include <string>
#include <set>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "Transform.hpp"

namespace Xern
{
	using boost::uuids::uuid;
	using boost::uuids::nil_uuid;
	namespace posix_time = boost::posix_time;

	struct IResource;
	struct IMovableObject;
	struct IMovementListener;
	struct ISceneNode;
	struct IRigidBody;
	class Region;
	class Entity: public boost::enable_shared_from_this<Entity>
	{
	public:
		typedef std::set<boost::shared_ptr<Entity> > Children;
		typedef std::set<boost::shared_ptr<IResource> > Resources;

		Entity();
		~Entity();

		virtual void Clear();

		const uuid &GetId() const;
		void SetId(const uuid &id);

		const std::string &GetName() const;
		void SetName(const std::string &name);

		const std::string &GetClassName() const;
		void SetClassName(const std::string &className);

		// Entity position and orientation.
		void SetTransform(const ReversibleTransform &transform);
		const ReversibleTransform &GetTransform() const;
		ReversibleTransform GetWorldTransform() const;

		// Linear velocity.
		void SetLinearVelocity(const Vector3 &velocity);
		const Vector3 &GetLinearVelocity() const;

		// Linear acceleration.
		void SetLinearAcceleration(const Vector3 &acceleration);
		const Vector3 &GetLinearAcceleration() const;

		// Angular velocity.
		void SetAngularVelocity(const Vector3 &velocity);
		const Vector3 &GetAngularVelocity() const;

		// Movement listener.
		void AddMovementListener(boost::shared_ptr<IMovementListener> listener);
		void RemoveMovementListener(boost::shared_ptr<IMovementListener> listener);

		boost::shared_ptr<ISceneNode> GetSceneNode();
		boost::shared_ptr<const ISceneNode> GetSceneNode() const;

		boost::shared_ptr<Entity> GetParent();
		boost::shared_ptr<const Entity> GetParent() const;

		virtual boost::shared_ptr<Region> GetRegion();
		virtual boost::shared_ptr<const Region> GetRegion() const;

		void SetRigidBody(boost::shared_ptr<IRigidBody> body);
		boost::shared_ptr<IRigidBody> GetRigidBody();
		boost::shared_ptr<const IRigidBody> GetRigidBody() const;

		int GetChildCount() const;
		void AddChild(boost::shared_ptr<Entity> child);
		void RemoveChild(boost::shared_ptr<Entity> child);
		const Children &GetChildren();
		boost::shared_ptr<Entity> FindChildByName(const std::string &name);
		boost::shared_ptr<Entity> FindChildByClassName(const std::string &classname);
		virtual boost::shared_ptr<Entity> FindChildById(const uuid &id);

		int GetResourceCount() const;
		void AddResource(boost::shared_ptr<IResource> resource);
		void RemoveResource(boost::shared_ptr<IResource> resource);
		const Resources &GetResources() const;

		virtual bool Frame(float delta);
		virtual void PhysicAction(float delta);
		virtual bool ParseProperty(const std::string &key, const std::string &value);
		virtual void Spawn();
		virtual void OnTouch(boost::shared_ptr<Entity> other);
		virtual void OnEvent(const std::string &name, const std::string &sender, const std::string &data);

		bool HasPhysicAction() const;
		void EnablePhysicAction();
		void DisablePhysicAction();

		// Some times.
		const posix_time::ptime &GetCreationTime() const;
		const posix_time::ptime &GetLastMovementTime() const;
		const posix_time::ptime &GetLastPhysicChangeTime() const;

	protected:
		virtual void CreateSceneNode();
		void SetSceneNode(boost::shared_ptr<ISceneNode> sceneNode);

	private:
		typedef std::set<boost::shared_ptr<IMovableObject> > Objects;
		typedef std::set<boost::shared_ptr<IMovementListener> > MovementListeners;

		void ChangeRegion(boost::shared_ptr<Region> oldRegion, boost::shared_ptr<Region> newRegion);
		void NotifyMovement(bool parent=false);
		void AddObject(boost::shared_ptr<IMovableObject> object);
		void RemoveObject(boost::shared_ptr<IMovableObject> object);

		friend class WorldManager;

		uuid guid;
		std::string name;
		std::string className;
		ReversibleTransform transform;
		Vector3 linearVelocity, linearAcceleration,angularVelocity;
		boost::weak_ptr<Entity> parent;

		Children children;
		Resources resources;
		Objects objects;
		MovementListeners movementListeners;

		// Scene node
		boost::shared_ptr<ISceneNode> sceneNode;

		// Rigid body.
		boost::shared_ptr<IRigidBody> rigidBody;
		bool hasPhysicAction;

		// Networking helpers.
		posix_time::ptime creationTime;
		posix_time::ptime lastMovementTime;
		posix_time::ptime lastPhysicChangeTime;
	};

}; // namespace Xern

#endif // XERN_ENTITY_HPP
